package com.example.phone.Login;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.phone.R;
import com.example.phone.base.BaseActivity;
import com.example.phone.base.RequestInterface;
import com.example.phone.base.ServerResponse;
import com.example.phone.utils.EditTextUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.iv_register_back)
    ImageView ivRegisterBack;
    @BindView(R.id.et_register_username)
    EditText etRegisterUsername;
    @BindView(R.id.iv_register_username_del)
    ImageView ivRegisterUsernameDel;
    @BindView(R.id.et_register_userPhone)
    EditText etRegisterUserPhone;
    @BindView(R.id.iv_register_userPhone_del)
    ImageView ivRegisterUserPhoneDel;

    @BindView(R.id.et_register_pwd_input)
    EditText etRegisterPwdInput;
    @BindView(R.id.iv_register_pwd_del)
    ImageView ivRegisterPwdDel;
    @BindView(R.id.et_register_auth_code)
    EditText etRegisterAuthCode;
    @BindView(R.id.tv_register_sms_call)
    TextView tvRegisterSmsCall;
    @BindView(R.id.bt_register_submit)
    Button btRegisterSubmit;

    EditTextUtil delete = new EditTextUtil();//删除输入框中的内容




    @Override
    public int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {


    }

    @OnClick({R.id.iv_register_back, R.id.tv_register_sms_call, R.id.bt_register_submit,
            R.id.iv_register_username_del, R.id.iv_register_userPhone_del,R.id.iv_register_pwd_del })
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.iv_register_back:
                goNextActivity(LoginActivity.class);
                break;
            case R.id.tv_register_sms_call:
                toastShort("验证码验证");
                break;
            case R.id.bt_register_submit:
                toastShort("提交注册信息");
                // TODO: 2018/12/25 验证这些值是不是合法的
                beginRegister();
                break;
            case R.id.iv_register_username_del:
                delete.clearButtonListener(etRegisterUsername, ivRegisterUsernameDel);//调用delete中的方法
                break;
            case R.id.iv_register_userPhone_del:
                delete.clearButtonListener(etRegisterUserPhone, ivRegisterUserPhoneDel);
                break;
            case R.id.iv_register_pwd_del:
                delete.clearButtonListener(etRegisterPwdInput, ivRegisterPwdDel);
                break;
        }
    }

    private void beginRegister() {
        String registerNameString = etRegisterUsername.getText().toString().trim();
        String registerPhoneString = etRegisterUserPhone.getText().toString().trim();
        String registerEmailString = null;
        String registerPwdString = etRegisterPwdInput.getText().toString().trim();

        if (TextUtils.isEmpty(registerNameString)) {
            toastShort("用户名不能为空！");
            return;
        }
        if (TextUtils.isEmpty(registerPhoneString)) {
            toastShort("手机号不能为空！");
            return;
        }
//        if (TextUtils.isEmpty(registerEmailString)) {
//            toastShort("邮箱地址不能为空！");
//            return;
//        }
        if (TextUtils.isEmpty(registerPwdString)) {
            toastShort("密码不能为空！");
            return;
        }
        doRegister(registerNameString, registerPwdString, registerEmailString, registerPhoneString);
    }


    //真正的注册逻辑
    private void doRegister(String registerNameString, String registerPhoneString, String registerEmailString, String registerPwdString) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface.Register requestInterfaceLogin = retrofit.create(RequestInterface.Register.class);
        Call<ServerResponse> call = requestInterfaceLogin.register(registerNameString, registerPhoneString, registerEmailString, registerPwdString);

        call.enqueue(new Callback<ServerResponse>() {

            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                /**
                 * response.body()获取到返回的ServerResponse，再通过获取Status判断是否
                 * 登陆成功/请求数据成功
                 */
                if (response.body() == null) {
                    toastShort("未获取到相关数据");
                    return;
                }
                //log(response.body().getMsg());
                if (response.body().getStatus() == 0) {
                    //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                    toastShort("注册成功");
                    goNextActivity(LoginActivity.class);//转跳到登陆页面
                    finish();
                } else {
                    toastShort("注册失败");
                }
            }


            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {
                Log.d(TAG, "onFailure: ");
                toastShort("注册失败！");
            }
        });
    }




}
